package com.ai.wxy.spring.jpa.test.service;

import com.ai.wxy.spring.auth.dto.UserDTO;
import com.ai.wxy.spring.jpa.test.security.UserRoleService;
import com.ai.wxy.spring.jpa.test.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 测试缓存
 *
 * @author 石头
 * @Date 2019/11/8
 * @Version 1.0
 **/
@Service
@Slf4j
public class SpringCacheService {
    @Autowired
    private UserRoleService userRoleService;
    @Cacheable(value = "key1")
    public String method1(){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("-----method1.get from db...---------");
        return "Hello,redis cahce";
    }

    @Cacheable(value = "userInfo")
    public UserVo findByName(UserDTO userDTO){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("-----findByName.get from db...---------");
        UserDTO user = userRoleService.findByName(userDTO.getUsername());
        UserVo userVo = new UserVo(user.getId(),user.getUsername(),null);

        return userVo;
    }
}
